/*
 * Copyright (c) 2018-2021. data2viz sàrl.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package com.gitee.wsl.common.struct.chord

//package io.data2viz.chord

import androidx.compose.ui.graphics.Path
import com.gitee.wsl.common.ui.ext.path.arc

import kotlin.math.PI
import kotlin.math.cos
import kotlin.math.sin


private val halfPi = PI / 2


/**
 * Generates a ribbon for a Chord Diagram with the given radius.
 */
 fun ribbon(radius: Float): (Chord, Path) -> Unit = { chord, path ->
    val s = chord.source
    val t = chord.target
    val sa0 = s.startAngle - halfPi
    val sa1 = s.endAngle - halfPi
    val sx0 = radius * cos(sa0).toFloat()
    val sy0 = radius * sin(sa0).toFloat()
    val ta0 = t.startAngle - halfPi
    val ta1 = t.endAngle - halfPi

    path.moveTo(sx0, sy0)

    path.arc(centerX = 0f, centerY = 0f, radius=radius, sa0.toFloat(), sa1.toFloat())
    if (sa0 != ta0 || sa1 != ta1) {
        path.quadraticBezierTo(0f, 0f, (radius * cos(ta0)).toFloat(), radius * sin(ta0).toFloat())
        path.arc(.0f, .0f, radius, ta0.toFloat(), ta1.toFloat())
    }
    path.quadraticBezierTo(.0f, .0f, sx0, sy0)
    path.close()
}
